<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: {
    // 总条数
    total: {
      required: true,
      type: Number
    },
    // 页
    page: {
      type: Number,
      default: 1
    },
    // 每页多少条
    limit: {
      type: Number,
      default: 100
    },
    layout: {
      type: String,
      default: "total, sizes, prev, pager, next, jumper"
    },
    // 是否用element背景
    background: {
      type: Boolean,
      default: true
    },
    // 是否隐藏分页
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      }
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit("update:limit", val);
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("chagngePagination", { page: this.currentPage, limit: val });
    },
    handleCurrentChange(val) {
      this.$emit("chagngePagination", { page: val, limit: this.pageSize });
    }
  }
};
</script>

<style scoped>
.pagination-container {
  background: #fff;
  /* padding: 32px 16px; */
}
.pagination-container.hidden {
  display: none;
}
</style>
